# -*- coding: utf-8 -*-
import cgi
import urllib
import httplib
from google.appengine.ext import webapp
from google.appengine.api import urlfetch
from google.appengine.ext.webapp.util import run_wsgi_app

class ProxyController(webapp.RequestHandler):

	def get(self):
	
		p = self.request.str_GET
		saida=''
		page = self.response.out
		
		if p.get('a','') == '':
			page.write("pagina nao existe")
		else:
			saida = p['a'] + '.php'
			p['a']=''
		
			params=urllib.urlencode(p)
			saida+='?'+params
			resultado = urlfetch.fetch(url = "http://www.dxs.com.br/app/" + saida,
									method= urlfetch.GET) 
									
			self.response.out.write(resultado.content)
			
	def post(self):
	
		p = self.request.str_POST
		saida=''
		page = self.response.out
		
		if p.get('a','')=='':
			page.write('pagina nao existe')
		else:
			saida=p['a']+'.php'
			p['a']=''
						
			try:
				params=urllib.urlencode(p)
				resultado = urlfetch.fetch(url = "http://www.dxs.com.br/app/" + saida,
									   payload=params,
									   method= urlfetch.POST,
									   headers={'Content-Type' : 'application/x-www-form-urlencoded'})
									   
				self.response.out.write(resultado.content)
				
			except:
				self.response.out.write("erro")
									   
			
application = webapp.WSGIApplication([('/xpto', ProxyController)],debug=True)

def main():
	run_wsgi_app(application)
 
if __name__ == "__main__":
	main()

